package cj.web.pay.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;

import org.springframework.beans.factory.annotation.Autowired;

import cj.web.cc.dao.ApplyDAO;
import cj.web.constants.MemuEnum;
import cj.web.constants.PayStatusEnum;
import cj.web.constants.PayWayEnum;
import cj.web.pay.alipay.config.AlipayConfig;
import cj.web.pay.alipay.interfaces.PayNotify;
import cj.web.pay.alipay.util.AlipayNotify;
import cj.web.pay.alipay.util.FmtRequestParams;
import cj.web.pay.alipay.util.PriceFormat;

@Path("applyNotify")
public class ApplyNotifyController extends PayNotify {
	@Autowired
	private ApplyDAO applyDao;

	@Post("ali")
	public void alipayNotify(@Param("out_trade_no") String out_trade_no,
			@Param("trade_no") String trade_no,
			@Param("trade_status") String trade_status,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		notify(out_trade_no, trade_no, trade_status, request, response);
	}

	/**
	 * 支付宝支付成功页面
	 * 
	 * @param model
	 * @param out_trade_no
	 * @param trade_status
	 * @param request
	 * @return
	 */
	@Get("aliScs")
	public String aliSuccess(Model model,
			@Param("out_trade_no") String out_trade_no,
			@Param("trade_status") String trade_status,
			HttpServletRequest request) {
		Map<String, String> params = FmtRequestParams.getRequestParams(request);
		model.add("currentTopMemu", MemuEnum.memuhdbm);
		model.add("payWay",PayWayEnum.ZFB.getValue());
		
		if (AlipayNotify.verify(params)) {// 验证成功
			if (trade_status.equals("TRADE_FINISHED")
					|| trade_status.equals("TRADE_SUCCESS")) {
				List<Map> orders = applyDao.getOrderByNo(out_trade_no);
				if (orders != null && orders.size() > 0) {
					Map order = orders.get(0);
					model.add("name", order.get("name").toString());
					model.add("schoolName", order.get("schoolName").toString());
					model.add("duration", order.get("duration").toString());
					model.add("code", order.get("code").toString());
					model.add("returnCode", order.get("returnCode").toString());
					return "/views/apply/paySuccess.vm";
				} else {
					return "/views/apply/payFail.vm";
				}
			} else {
				return "/views/apply/payFail.vm";
			}
		} else {// 验证失败
			return "/views/apply/payFail.vm";
		}
	}

	@Override
	public void tradeFinished(String out_trade_no, String trade_no) {
		List<Map> orders = applyDao.getOrderByNo(out_trade_no);
		if (orders != null && orders.size() > 0) {
			Map order = orders.get(0);
			int status = Integer.parseInt(order.get("status").toString());
			Object payOrder = order.get("payOrder");
			if (status == PayStatusEnum.WFK.getValue()
					&& (payOrder == null || "".equals(payOrder.toString()))) {
				applyDao.updatePayStatusByNo(out_trade_no, trade_no,
						PayStatusEnum.YFK.getValue(), PayWayEnum.ZFB.getValue()); // 修改订单状态为已付款
																					// 更新支付订单号
																					// 支付方式
			}
		}
	}

	@Override
	public void tradeSuccess(String out_trade_no, String trade_no) {
		tradeFinished(out_trade_no, trade_no);
	}

	@Override
	public boolean isNotifyInfoEqual(String outTradeNo, String notifyTotalFee,
			String notifySellerId) {
		List<Map> orders = applyDao.getOrderByNo(outTradeNo);
		if (orders != null && orders.size() > 0) {
			Map order = orders.get(0);
			String price = PriceFormat.formatPrice(order.get("price"));
			if (notifyTotalFee != null && notifySellerId != null
					&& notifyTotalFee.equals(price)
					&& notifySellerId.equals(AlipayConfig.seller_id)) {
				return true;
			}
		}
		return false;
	}
}